package net.web.milkteapos.module;



import java.util.ArrayList;
import java.util.List;

import net.web.milkteapos.bean.Product;
import net.web.milkteapos.bean.ProductType;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.pager.Pager;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.lang.util.NutMap;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Fail;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import net.web.milkteapos.bean.Money;
import net.web.milkteapos.bean.Product;
@IocBean
@At("/product_type")
@Ok("json")
@Fail("http:500")
public class ProductTypeModule {

	@Inject
	protected Dao dao;
	/**
	 * 新增
	 * @param product_type
	 * @return
	 */
	@At
	public Object add(@Param("..")ProductType product_type) {
		NutMap re = new NutMap();
        String msg = checkProductType(product_type, true);
        if (msg != null){
            return re.setv("ok", false).setv("msg", msg);
        }
        product_type = dao.insert(product_type);
        return re.setv("ok", true).setv("data", product_type);
	}


	/**
	 * 修改
	 * @param product_type
	 * @return
	 */
	@At
	public Object update(@Param("..")ProductType product_type) {
		NutMap re = new NutMap();
        String msg = checkProductType(product_type, false);
        if (msg != null){
            return re.setv("ok", false).setv("msg", msg);
        }
 
        dao.updateIgnoreNull(product_type);
        return re.setv("ok", true);
	}
	
	
	/**
	 * 查询
	 * @param name
	 * @param pager
	 * @return
	 */
	
	/*@At
    public Object query(@Param("name")String name, @Param("..")Pager pager) {
        Cnd cnd = Strings.isBlank(name)? null : Cnd.where("type_name", "like", "%"+name+"%");
        QueryResult qr = new QueryResult();
        qr.setList(dao.query(ProductType.class, cnd, pager));
        pager.setRecordCount(dao.count(ProductType.class, cnd));
        qr.setPager(pager);
        return qr; //默认分页是第1页,每页20条
    }*/

	
	/**
	 * 查询
	 * @param name
	 * @param pager
	 * @return
	 */
	@At
    public Object query_all() {
        QueryResult qr = new QueryResult();
        qr.setList(dao.query(ProductType.class, null));
        return qr;
    }
	
	/**
	 * 查询
	 * @param name
	 * @param pager
	 * @return
	 */
	@At
    public Object query_all_with_product() {
        QueryResult qr = new QueryResult();
        List<ProductType> types = new ArrayList<>();
        List<ProductType> result = dao.query(ProductType.class, null);
        for (ProductType model : result) {
			dao.fetchLinks(model, "products");
			types.add(model);
		}
        qr.setList(types);
        return qr;
    }

	public static void main(String[] args){
		Object product = null;
		System.out.println(((ProductTypeModule) product).query_without_pager("红"));
		
	}
	@At
	 public Object query_without_pager(@Param("name")String name) {
        Cnd cnd = Strings.isBlank(name)? null : Cnd.where("type_name", "like", "%"+name+"%");
        QueryResult qr = new QueryResult();
        List<ProductType> productTypes = new ArrayList<>();
        List<ProductType> result = dao.query(ProductType.class, cnd);
        for (ProductType model : result) {
			dao.fetchLinks(model, "typeName");
			productTypes.add(model);
		}
        qr.setList(productTypes);
        return qr;
    }
	 
	@At
    public Object query_full(@Param("id")int id) {
		 Cnd cnd = Cnd.where("type_id", "=", id);
        ProductType type = dao.fetch(ProductType.class, cnd);
        dao.fetchLinks(type, "productTypes");
        return type; 
    }

	
	/**
	 * 删除
	 * @param type_id
	 * @return
	 */
	@At
    public Object delete(@Param("type_id")int type_id) {
		System.out.println(type_id);
        if (type_id < 1) {
            return new NutMap().setv("ok", false).setv("msg", "商品ID非法");
        }
        dao.delete(ProductType.class, type_id);
        return new NutMap().setv("ok", true);
    }
	
	
	@At
    public int count() {
        return dao.count(ProductType.class);
    }
	
	private String checkProductType(ProductType product_type, boolean create) {
		if(product_type == null) {
			return "空对象";
		}
		if(!create && product_type.getTypeId() < 1) {
			return "商品ID非法";
		}
		return null;
	}
}